// pages/promotion/modify/modify.js
var recommendservice = require("../../../apiservice/recommend/recommendservice");
const jsonData = require("./jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const common = require("./common.js");
const util = require("../../../utils/util.js");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		genderOpt: jsonData.Gender, //性别选项集
		contentOpt: jsonData.Content, //主要需求选项集
		genderindex: 0, //性别索引
		contentindex: 0, //主要需求索引
		Recommend: [], //推荐记录
		hospitalList: [], //所有医院列表
		hospitalindex: 0, //医院索引
		appointmenttothehospitaldate: '请选择', //预约日期
		leadName: '', //潜在客户姓名
		telephone: '', //电话
		age: '', //年龄
		idnum: '', //身份证
		remark: '', //备注
		hospital: [], //医院
		hospitalid: '', //医院ID
		doctor: [], //医生
		doctorid: '', //医生ID
		doctorHospitalid: '', //医生所在医院
		telephone2: '',
		birthdate: '',
    currDate: util.frmatDate(new Date())
	},
	changeGender: function(e) {
		console.log(e);
		this.setData({
			genderindex: e.detail.value

		});
	},
	changeHospital: function(e) {
		console.log(e);
		this.setData({
			hospitalindex: e.detail.value,
			hospitalid: this.data.hospitalList[e.detail.value].siteId,
		});
	},
	changeContent: function(e) {
		console.log(e);
		this.setData({
			contentindex: e.detail.value,
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		var result = await this.QueryRecommendById(options.id);
		debugger;
		if (result.new_birthdate && result.new_birthdate.length>10)
			result.new_birthdate = result.new_birthdate.substring(0, 10);
		var contentindex = common.getContent(result.new_content);
		var hospitalList = await this.GetAllHospital();
		var index = common.getPickerValue(hospitalList, options.hosid)
		console.log(options);
		console.log("index:" + index);

		var genderindex = 0;
		if (result.new_gender) {
			genderindex = result.new_gender;
		}
		console.log(index);
		this.setData({
			Recommend: result,
			genderindex: genderindex,
			contentindex: contentindex,
			hospitalList: hospitalList,
			hospitalindex: index,
			hospitalid: hospitalList[index].siteId,
			doctorid: result.new_contact,
			appointmenttothehospitaldate: result.new_appointmenttothehospitaldate>10
			 ? result.new_appointmenttothehospitaldate.substring(0, 10) : result.new_appointmenttothehospitaldate
		});
	},
	//监听字段更改
	onNameChange: function(e) {
		var result = 'Recommend.new_leadName';
		this.setData({
			[result]: e.detail
		})
	},
	onPhoneChange: function(e) {
		var result = 'Recommend.address1_Telephone1'
		this.setData({
			[result]: e.detail
		})
	},
	onAgeChange: function(e) {
		var result = 'Recommend.new_age';
		this.setData({
			[result]: e.detail
		})
	},
	onCitizenidChange: function(e) {
		var result = 'Recommend.new_citizenid'
		this.setData({
			[result]: e.detail
		})
	},
	onRemarkChange: function(e) {
		var result = 'Recommend.new_remark';
		this.setData({
			[result]: e.detail.value
		})
	},
	onDateChange: function(e) {
		this.setData({
			appointmenttothehospitaldate: e.detail.value
		})
	},
	changeBirthdate: function(e) {
		this.setData({
			['Recommend.new_birthdate']: e.detail.value
		})
	},
	changeTelephone2: function(e) {
		this.setData({
			['Recommend.telephone2']: e.detail
		});
	},
	toSelectDoctor: function() {
		var that = this;
		wx.navigateTo({
			url: "../../doctor/list/list",
			events: {
				openedDoctorPage: function(e) {

				},
				DoctorEvent: function(data) {
					console.log(data)
					that.setData({
						doctorid: data.doctorid,
						doctorname: data.doctorname,
            doctorHospitalid: data.hospitalid, 
            ['Recommend.new_contact']: data.doctorid,
            ['Recommend.new_contactName']: data.doctorname
					});
				}
			},
			success: function(res) {
				res.eventChannel.emit('openedDoctorPage', {
					data: 'choose'
				})
			}
		})
	},
	//修改推荐记录
	modRecommond: function(e) {
		//debugger;
		var contentValue = common.getContentValue(this.data.contentindex);
		console.log(contentValue);
		var recommendobj = {
			new_recommendId: this.data.Recommend.new_recommendId,
			new_lead: this.data.Recommend.new_lead,
			new_leadName: this.data.Recommend.new_leadName,
			Address1_Telephone1: this.data.Recommend.address1_Telephone1,
			new_gender: this.data.genderindex,
			new_age: this.data.Recommend.new_age,
			new_citizenid: this.data.Recommend.new_citizenid,
			new_content: contentValue,
			new_remark: this.data.Recommend.new_remark,
			new_hospital: this.data.hospitalid,
			new_appointmenttothehospitaldate: this.data.appointmenttothehospitaldate,
			new_contact: this.data.doctorid,
			new_networkhospitalwherethehospital: this.data.doctorHospitalid,
			telephone2: this.data.Recommend.telephone2,
			new_birthdate: this.data.Recommend.new_birthdate
		};
		var msg = '';
		var isCreate = true;
		var flag = true;
		if (recommendobj.new_leadName.replace(/\s+/g, '') == '' && flag == true) {
			msg = '姓名不能为空';
			isCreate = false;
			flag = false;
		}
    if (recommendobj.Address1_Telephone1 == '' && flag == true) {
      msg = '手机号码不能为空';
      isCreate = false;
      flag = false;
    }
		if (recommendobj.Address1_Telephone1 != '' && recommendobj.Address1_Telephone1 != undefined) {
			if (!(/^1[3456789]\d{9}$/.test(recommendobj.Address1_Telephone1)) && flag == true) {
				msg = '手机号码不正确';
				isCreate = false;
				flag = false;
			}
		}
		if (recommendobj.new_age != undefined) {
			if (recommendobj.new_age > 150) {
				msg = '请填写正确的年龄';
				isCreate = false;
				flag = false;
			}
		}
		if (recommendobj.new_contact == '' && flag == true) {
			msg = '推广医生姓名不能为空';
			isCreate = false;
			flag = false;
		}
		if (isCreate == false) {
			wx.showToast({
				title: msg,
				icon: 'none',
				duration: 2000
			})
		} else {
			console.log(recommendobj);
			this.UpdateRecommendAndLead(recommendobj);
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 修改推荐记录信息
	UpdateRecommendAndLead: function(recommend) {
		var that = this;
		recommendservice.UpdateRecommendAndLead(recommend).then(function(success) {
			that.showResult(success.data);
			wx.redirectTo({
				url: '/pages/promotion/detail/detail?id=' + that.data.Recommend.new_recommendId
			})
		}).catch(function(ex) {
			console.log(ex);
		})

	},
	// 根据记录ID获取记录
	QueryRecommendById: async function(id) {
		var result = await recommendservice.QueryRecommendById(id)
		return result.data
	},
	//获取所有医院
	GetAllHospital: async function() {
		var result = await recommendservice.GetAllHospital()
		return result.data
	},
	showResult: function(res) {
		var title = '';
		var icon = '';
		if (res.resultState == 1) {
			title = '修改成功';
			icon = 'success';
		} else {
			title = '修改失败';
			icon = 'none';
		}
		wx.showToast({
			title: title,
			icon: icon,
			duration: 3000
		});
	},
})
